package com.sdp.right.db.sys_import_template.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.right.db.sys_import_template.mapper.SysImportTemplateMapper;
import com.sdp.right.db.sys_import_template.service.ISysImportTemplateService;
import com.sdp.right.db.sys_import_template_detail.service.ISysImportTemplateDetailService;
import com.sdp.right.entity.SysImportTemplate;
import com.sdp.right.entity.SysImportTemplateDetail;
import com.sdp.right.service.RemoveSystemService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2023-10-18
 */
@Service
public class SysImportTemplateServiceImpl extends ServiceImpl<SysImportTemplateMapper, SysImportTemplate> implements ISysImportTemplateService,RemoveSystemService {

	@Autowired
	private ISysImportTemplateDetailService iSysImportTemplateDetailService;
	
	
	@Override
	public SysImportTemplate querySysImportTemplateById(String templateId) {
		LambdaQueryWrapper<SysImportTemplate> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysImportTemplate::getTemplateId, templateId);
		return getOne(queryWrapper,false);
	}

	@Override
	public void removeBySystemNo(String systemNo) {
		LambdaQueryWrapper<SysImportTemplate> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysImportTemplate::getSystemNo, systemNo);
		List<SysImportTemplate> list = list(queryWrapper);
		List<String> templateIds = new ArrayList<String>();
		if(list!=null && list.size()>0) {
			for(SysImportTemplate template : list) {
				templateIds.add(template.getTemplateId());
			}
			LambdaQueryWrapper<SysImportTemplateDetail> detailQueryWrapper = new LambdaQueryWrapper<>();
			detailQueryWrapper.in(SysImportTemplateDetail::getTemplateId, templateIds);
			iSysImportTemplateDetailService.remove(detailQueryWrapper);
			remove(queryWrapper);
		}
		
		
	}

}
